<?php

namespace App\Http\Controllers\Admin;

use App\Models\Activity;
use App\Models\Ad;
use App\Models\AdPosition;

use App\Http\Requests\Admin\Ad\AdRequest;

use Illuminate\Http\Request;

use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Session;
use App\Http\Controllers\Controller;
use Gate;
use App\Exceptions\Admin\PermissionDeniedException;
use App\Commands\OperationLogCommand;

class AdController extends Controller
{

    public function ad_gate()
    {
        if (!Gate::allows('ad','')) {
            throw new PermissionDeniedException();
        }//end if()

    }//end func __construce()
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $this->ad_gate();

        $pos_id = Session::get('admin_ad_position_id');

        if(!empty($pos_id)) {
           return $this->show($request, $pos_id);
        } else {
            return;
        }
    }

    public function show(Request $request, $id = 0){

        $this->ad_gate();

        Session::put('admin_ad_position_id', $id);

        if($id == 0) {

            $ads = Ad::paginate(10);

        } else {

            $ads = Ad::where('pos_id', '=', $id)->orderBy('list_order', 'asc')->paginate(10);

        }

        $adposition = AdPosition::paginate(10);

        foreach($adposition as $adpositonArray) {

            $adpos[$adpositonArray->pos_id]=$adpositonArray->position_name;

        }

        return view('admin.ad.index', compact(['ads', 'adpos', 'id']));

    }


    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Ad $ad)
    {

        $this->ad_gate();

        $activity = Activity::all();

        foreach ($activity as $key => $value) {
            $activity_id_arr[$value->id] = $value->id;
        }
        //dd($activity_identification_arr);
        $adposition = AdPosition::all();

        foreach($adposition as $adp) {

            $adpos[$adp->pos_id]=$adp->position_name;

        }

        $old_pos_id = Session::get('admin_ad_position_id');

        return view('admin.ad.create',compact(['ad' , 'adpos', 'old_pos_id','activity_id_arr']));

    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(AdRequest $storeRequest,Ad $ad)
    {
        $this->ad_gate();
        $old_pos_id = $storeRequest->get('pos_id');

        $param = $storeRequest->all();
        $param['m_url'] =config('domain.mobile')."/activity/".$param['m_url'];
        $ad = $ad->create($param);

        $this->dispatch(
            new OperationLogCommand('添加广告','Ad','id:'.$ad->id)
        );


        return Redirect('admin/ad/'.$old_pos_id);

    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $this->ad_gate();

        $activity = Activity::all();
        $activity_id_arr[0]="请选择活动链接";
        foreach ($activity as $key => $value) {
            $activity_id_arr[$value->id] = $value->id;

        }

        //dd($activity_id_arr);

        $ad = Ad::find($id);

        if($ad->m_url){

            $activity_id = explode('/',$ad->m_url)[2];
        }else{
            $activity_id = 0;
        }

        $adposition = AdPosition::all();

        foreach($adposition as $adp) {

            $adpos[$adp->pos_id]=$adp->position_name;

        }

        $old_pos_id = Session::get('admin_ad_position_id');

        return view('admin.ad.edit',compact(['ad' , 'adpos', 'old_pos_id','activity_id_arr','activity_id']));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(AdRequest $request, $id)
    {

        $this->ad_gate();

        $ad = Ad::find($id);

        $param = $request->all();
        //dd(env('MOBIL_DOMAIN'));
        $param['m_url'] = config('domain.mobile')."/activity/".$param['m_url'];

        $ad->update($param);

        $old_pos_id = $request->get('pos_id');
        $this->dispatch(
            new OperationLogCommand('修改广告','Ad','id:'.$id)
        );

        return Redirect('admin/ad/'.$old_pos_id);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $this->ad_gate();

        $return = ['status'=>0, 'message'=>'', 'data'=>''];
        $ad = Ad::find($id);

        $ad->delete();
        $this->dispatch(
            new OperationLogCommand('删除广告','Ad','id:'.$id)
        );

        $old_pos_id = Session::get('admin_ad_position_id');
        return $return;
        //return Redirect('admin/ad'.$old_pos_id);
    }


    public function sort(Request $request){

        $this->ad_gate();

        $ads = $request->get('list_order');

        $pos_id = $request->get('id');

        $index = $request->get('id');

        foreach ($ads as $id => $list_order) {

            Ad::find($id)->update(['list_order'=>$list_order]);

        }

        return Redirect('admin/ad/'.$pos_id);

    }
}
